<?php
add_action('widgets_init', 'register_slider_home_widget');

function register_slider_home_widget() {
    register_widget('slider_home');
}

class slider_home extends WP_Widget // widget class 
{
    function slider_home() { // widget setting | class name must be same function name
        $widget_ops = array('classname' => 'slider-links-w', 'description' =>
            'Display Slide at Home page');
        $control_ops = array(
            'width' => 250,
            'height' => 350,
            'id_base' => 'slider_home');
        $this->WP_Widget('slider_home', 'DealX - Slider', $widget_ops, $control_ops);
    }
    
    function widget($args, $instance) {
        $title = $instance['title'];
        $count_frame = $instance['count_frame'];
        $today = date('Y/m/d');
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => $count_frame*4,
            'orderby' => 'meta_value',
            'meta_key' => 'start_date_metadata',
            'order' => 'ASC',
            'post__not_in' => get_option( 'sticky_posts' ),
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key' => 'start_date_metadata',
                    'value' => $today,
                    'compare' => '<='
                ),
                array(
                    'key' => 'end_date_metadata',
                    'value' => $today,
                    'compare' => '>='
                )
            )
        );
        $query = new WP_Query($args);
        $post = $query->posts;
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                $('#carousel').elastislide({
                    orientation: 'horizontal'
                    ,speed: 1000
                    ,autoplay : true
                    ,interval : 6000
                });
            });
        </script>
        <div class="slider-home-wrap row-fluid">
             <div class="hp-others-sub-title">
                 <span class="title"><?php echo $title;?></span>
                 <span class="see-all"><a href="<?php echo site_url().'/all-deals';?>">See all deals</a></span>
             </div>
             <div class="slider-home">
                 <ul id="carousel" class="elastislide-list">
                     <?php 
                        foreach ($post as $p):
                            $post_name = $p->post_title;
                            $image = get_post_meta($p->ID, 'deal_feature_metadata', true);
                            $end_date = get_post_meta($p->ID, 'end_date_metadata', true);
                        ?>
                            <li>
                                <span class="image-wrap">
                                    <a href="<?php echo get_permalink($p->ID);?>" title="<?php echo $post_name;?>">
                                        <img src="<?php echo $image;?>" title="<?php echo $post_name;?>"/>
                                    </a>
                                </span>
                                <div class="recent-deal-title">
                                     <a href="<?php echo get_permalink($p->ID);?>" title="<?php echo $post_name;?>"><?php echo $post_name;?></a>
                                </div>
                                <div class="another-timer-block">
                                    <div id="count_down_<?php echo $p->ID;?>" class="time-count">
                                        <?php cpx_get_count_down($p->ID, $end_date); ?>
                                    </div>
                                </div>
                            </li>
                        <?php endforeach;
                     ?>
                 </ul>
             </div>
        </div>                
    <?php }
    function update($new_instance, $old_instance) { // update widget
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['count_frame'] = $new_instance['count_frame'];
        return $instance;
    }
    function form($instance) { // form for the widget options  
    ?>
    <div style="color: #333;">
            <p>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo 'Title'; ?></label>
                <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
            </p>
            <p>
    		<label for="<?php echo $this->get_field_id('count_frame');?>"><?php echo 'Enter number frame display ';?></label>
    		<input type="text" id="<?php echo $this->get_field_id('count_frame');?>" name="<?php echo $this->get_field_name('count_frame');?>" value="<?php echo $instance['count_frame'];?>" style="width:90%;" />
            </p>
    </div>
    <?php    
    }
}
?>
